package com.ocom.company.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.company.EntityFlowApprover;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.company.*;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.GetApproverListByFlowVo;
import com.ocom.company.service.FlowApproverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class FlowApproverController {

    @Autowired
    private FlowApproverService flowApproverService;

    @PostMapping(CompanyServiceUrl.URL_getApproverListByFlow)
    public Result getApproverListByFlow(@RequestBody GetApproverListByFlowRequest getApproverListByFlowRequest){
        List<GetApproverListByFlowVo> getApproverListByFlowVoList = new ArrayList<>();
        this.flowApproverService.getByFid(getApproverListByFlowRequest.getComId(),getApproverListByFlowRequest.getFlowId()).forEach(entityFlowApprover -> {
            GetApproverListByFlowVo getApproverListByFlowVo = Convert.convert(GetApproverListByFlowVo.class,entityFlowApprover);
            getApproverListByFlowVoList.add(getApproverListByFlowVo);
        });

        return ResultUtil.success(getApproverListByFlowVoList);
    }

    @PostMapping(CompanyServiceUrl.URL_newFlowApprover)
    public Result newFlowApprover(@RequestBody NewFlowApproverRequest newFlowApproverRequest){
        EntityFlowApprover entityFlowApprover = Convert.convert(EntityFlowApprover.class,newFlowApproverRequest);
        EntityFlowApprover entityFlowApproverA = this.flowApproverService.getOne(entityFlowApprover.getComId(),entityFlowApprover);
        if(ObjectUtil.isNotEmpty(entityFlowApproverA)){
            return ResultUtil.error(ResultEnum.Err_5004);
        }
        return ResultUtil.success(this.flowApproverService.save(entityFlowApprover.getComId(),entityFlowApprover));
    }

    @PostMapping(CompanyServiceUrl.URL_updateFlowApprover)
    public Result updateFlowApprover(@RequestBody UpdateFlowApproveRequest updateFlowApproveRequest){
        EntityFlowApprover entityFlowApprover = Convert.convert(EntityFlowApprover.class,updateFlowApproveRequest);
        return ResultUtil.success(this.flowApproverService.update(entityFlowApprover.getComId(),entityFlowApprover));
    }

    @PostMapping(CompanyServiceUrl.URL_removeFlowApprover)
    public Result removeFlowApprover(@RequestBody RemoveFlowApproverRequest removeFlowApproverRequest){
        EntityFlowApprover entityFlowApprover = Convert.convert(EntityFlowApprover.class,removeFlowApproverRequest);
        return ResultUtil.success(this.flowApproverService.remove(entityFlowApprover.getComId(),entityFlowApprover));
    }

    @PostMapping(CompanyServiceUrl.URL_getFlowApprovalDetail)
    public Result getFlowApprovalDetail(@RequestBody GetFlowApprovalDetailRequest getFlowApprovalDetailRequest){
        return ResultUtil.success(this.flowApproverService.getFlowApprovalDetail(getFlowApprovalDetailRequest.getComId(), getFlowApprovalDetailRequest));
    }
}
